package com.hulk.hyena.project.system.post.service;

import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;

import com.hulk.hyena.common.utils.StringUtils;
import com.hulk.hyena.common.utils.TypeUtils;
import com.hulk.hyena.framework.exception.HyenaException;
import com.hulk.hyena.project.system.user.domain.SysUserPost;
import com.hulk.hyena.project.system.user.service.IUserPostService;
import com.hulk.hyena.framework.service.impl.BaseServiceImpl;
import com.hulk.hyena.project.system.post.domain.SysPost;
import com.hulk.hyena.project.system.post.mapper.PostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 岗位信息 服务层处理
 *
 * @author hulk
 */
@Service
public class PostServiceImpl extends BaseServiceImpl<PostMapper, SysPost> implements IPostService {

    @Autowired
    private IUserPostService userPostService;

    @Override
    public List<SysPost> selectPostList(SysPost post) {
        return query().like(StringUtils.isNotEmpty(post.getPostCode()), SysPost::getPostCode, post.getPostCode())
                .eq(StringUtils.isNotEmpty(post.getStatus()), SysPost::getStatus, post.getStatus())
                .like(StringUtils.isNotEmpty(post.getPostName()), SysPost::getPostName, post.getPostName())
                .list();
    }

    @Override
    public List<SysPost> selectAllPostsByUserId(Long userId) {
        List<SysPost> userPosts = selectPostsByUserId(userId);
        List<SysPost> posts = list();
        for (SysPost post : posts) {
            for (SysPost userRole : userPosts) {
                if (post.getPostId().longValue() == userRole.getPostId().longValue()) {
                    post.setFlag(true);
                    break;
                }
            }
        }
        return posts;
    }

    @Override
    public List<SysPost> selectPostsByUserId(Long userId) {
        return baseMapper.selectPostsByUserId(userId);
    }

    @Override
    public boolean deletePostByIds(String ids) {
        List<Long> postIds = StringUtils.split2List(ids, TypeUtils::castToLong);
        for (Long postId : postIds) {
            SysPost post = getById(postId);
            if (userPostService.query().eq(SysUserPost::getPostId, postId).exist()) {
                throw new HyenaException(HttpServletResponse.SC_BAD_REQUEST, post.getPostName() + "已分配，不能删除");
            }
        }
        return delete().in(SysPost::getPostId, postIds).execute();
    }

    @Override
    public boolean checkPostNameUnique(SysPost post) {
        Long postId = post.getPostId();
        SysPost info = query().eq(SysPost::getPostName, post.getPostName()).getOne();
        return Objects.isNull(info) || info.getPostId().equals(postId);
    }

    @Override
    public boolean checkPostCodeUnique(SysPost post) {
        Long postId = post.getPostId();
        SysPost info = query().eq(SysPost::getPostCode, post.getPostCode()).getOne();
        return Objects.isNull(info) || info.getPostId().equals(postId);

    }
}
